/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.vod.gauss.matrice;

/**
 *
 * @author drazanakoto
 */
public class EquationImpl implements Equation {

    protected int[] ligne;
    protected int result;

    /**
     *
     * @param e
     */
    public EquationImpl(Equation e) {
        this(e.getLigne(), e.getResult());
    }

    /**
     *
     * @param ligne
     * @param result
     */
    public EquationImpl(int[] ligne, int result) {
        this.ligne = ligne;
        this.result = result;
    }

    /**
     *
     * @param index
     * @return
     */
    @Override
    public int get(int index) {
        return ligne[index];
    }

    /**
     *
     * @param index
     * @param value
     */
    @Override
    public void setValueAt(int index, int value) {
        ligne[index] = value;
    }

    /**
     *
     * @param resultat
     */
    @Override
    public void setResult(int resultat) {
        this.result = resultat;
    }

    /**
     *
     * @return
     */
    @Override
    public int getResult() {
        return result;
    }

    /**
     *
     * @return
     */
    @Override
    public int getSize() {
        return ligne.length;
    }

    @Override
    public int[] getLigne() {
        return ligne.clone();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int i : ligne) {
            sb.append(i < 0 ? i : "+" + i);
        }
        return sb.toString().substring(1) + "=" + result;
    }

    @Override
    public int isSolve() {
        int nbZero = 0;
        int nbOther = 0;
        for (int i = 0; i < ligne.length; i++) {
            switch (ligne[i]) {
                case 0:
                    nbZero++;
                    break;
                default:
                    nbOther++;
            }
        }

        if (nbZero == ligne.length - 1 && nbOther == 1) {
            for (int i = 0; i < ligne.length; i++) {
                if (ligne[i] != 0) {
                    return i;
                }
            }
        }
        return -1;
    }
}
